package com.cskaoyan.service;


import com.cskaoyan.mapper.*;

import com.cskaoyan.mapper.MarketGoodsMapper;

import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.GoodsData;
import com.cskaoyan.module.bo.goods.CreateGoodsBo;
import com.cskaoyan.module.bo.goods.UpdateGoodsBo;
import com.cskaoyan.module.pojo.*;
import com.cskaoyan.module.vo.goods.*;
import com.cskaoyan.module.bean.param.BaseParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/04/06 20:27
 *
 * @author hxing
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Transactional
    @Override
    public GoodsData query(SeekGoodsList seekGoodsList, BaseParam baseParam) {
        Integer page = baseParam.getPage(); // 页码
        Integer limit = baseParam.getLimit(); // 数据量
        String sort = baseParam.getSort(); // 排序列
        String order = baseParam.getOrder(); // dsec\asc

        // 分页插件 PageHelper,辅助我们作分页和分页信息的获得
        //执行查询过程中拼接分页信息
        PageHelper.startPage(page, limit);

        MarketGoodsExample example = new MarketGoodsExample();

        //使用这个createCriteria方法创建Example的内部类对象 → 里面维护了条件列表
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        String goodsSn = seekGoodsList.getGoodsSn();
        String name = seekGoodsList.getName();
        Integer goodsId = seekGoodsList.getGoodsId();

        if (goodsSn != null && !"".equals(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andIdEqualTo(goodsId);
        }
        example.setOrderByClause(sort + " " + order);

        List<MarketGoods> list = marketGoodsMapper.selectByExample(example);

        PageInfo<MarketGoods> marketGoodsPageInfo = new PageInfo<>(list);
        long total = marketGoodsPageInfo.getTotal();
        int pages = marketGoodsPageInfo.getPages();
        return GoodsData.data(total, pages, limit, page, list);
    }
    @Transactional
    @Override
    public CartAndBrandVo seekCatAndBrand() {
        CartAndBrandVo result = new CartAndBrandVo();


//1/L1 -(label -id   value  -name)L2-()

        MarketCategoryExample example=new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);//pid=0的category的id们
        CategoryByCatAndBrandVO categoryByCatAndBrandVO = new CategoryByCatAndBrandVO();

        List<CategoryByCatAndBrandVO> categoryList = new ArrayList<>();
        List<BrandListByCatAndBrandVO> brandList =new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
//            拿到id ->遍历id -> 作为参数pid等于这个id  -> 拿到id和name
//            建一个一级的 传id和name
            CategoryByCatAndBrandVO list1Inner = new CategoryByCatAndBrandVO();
            list1Inner.setLabel(marketCategory.getName());//value
            list1Inner.setValue(marketCategory.getId());//label
            MarketCategoryExample exampleInner=new MarketCategoryExample();
            MarketCategoryExample.Criteria criteriaInner = exampleInner.createCriteria();
            criteria.andPidEqualTo(marketCategory.getId());
            List<MarketCategory> listCategoryInner = marketCategoryMapper.selectByExample(exampleInner);
            List<CategoryByChildrenVO> childrenCategoryList=new ArrayList<>();//children
            for (MarketCategory category : listCategoryInner) {

                CategoryByChildrenVO categoryByChildrenVO = new CategoryByChildrenVO();
                categoryByChildrenVO.setLabel(category.getName());
                categoryByChildrenVO.setValue(category.getId());
                childrenCategoryList.add(categoryByChildrenVO);//children
            }
            list1Inner.setChildren(childrenCategoryList);
            categoryList.add(list1Inner);

        }

        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(null);
        for (MarketBrand marketBrand : marketBrands) {
            BrandListByCatAndBrandVO brandListByCatAndBrandVO = new BrandListByCatAndBrandVO();
            brandListByCatAndBrandVO.setLabel(marketBrand.getName());
            brandListByCatAndBrandVO.setValue(marketBrand.getId());
            brandList.add(brandListByCatAndBrandVO);
        }
        result.setCategoryList(categoryList);
        result.setBrandList(brandList);
        System.out.println("result="+result);
        return result;

    }
    @Transactional
    @Override
    public GoodsDetailVo seekDetailById(Integer id) {
        MarketGoods goods = marketGoodsMapper.selectByPrimaryKey(id);
        Integer categoryId = goods.getCategoryId();
        ArrayList<Integer> categoryIds = new ArrayList<>();
        categoryIds.add(1008000);
        categoryIds.add(categoryId);

        MarketGoodsAttributeExample example1 = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> attributes = marketGoodsAttributeMapper.selectByExample(example1);


        MarketGoodsSpecificationExample example2 = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specifications = marketGoodsSpecificationMapper.selectByExample(example2);


        MarketGoodsProductExample example3 = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria3 = example3.createCriteria();
        criteria3.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> products = marketGoodsProductMapper.selectByExample(example3);

        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setGoods(goods);
        goodsDetailVo.setAttributes(attributes);
        goodsDetailVo.setSpecifications(specifications);
        goodsDetailVo.setProducts(products);
        goodsDetailVo.setCategoryIds(categoryIds);

        return goodsDetailVo;
    }

    @Transactional
    @Override


    public GoodsListLimitVo listLimit0(int limit) {
        GoodsListLimitVo result = new GoodsListLimitVo();

        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
//        仅当删除状态为false才参与详情展示
        criteria.andDeletedEqualTo(false);

        List<MarketGoods> list = marketGoodsMapper.selectByExample(example);
        result.setList(list);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        int pages = pageInfo.getPages();
//        Integer total = ((Integer) pageInfo.getTotal());
        int pageNum = pageInfo.getPageNum();

        result.setPage(pageNum);
        result.setPages(pages);
        result.setTotal((int) pageInfo.getTotal());
        result.setLimit(pageInfo.getSize());
        System.out.println("result = " + result);
        return result;

    }
    @Transactional
    @Override
    public BaseRespVo createGoods(CreateGoodsBo createGoodsBo) {
        // 通过传入的参数获取goods信息
        MarketGoods goods = createGoodsBo.getGoods();
        // 再获取 specification
        List<MarketGoodsSpecification> specifications = createGoodsBo.getSpecifications();
        // 获取 product信息
        List<MarketGoodsProduct> products = createGoodsBo.getProducts();
        // 获取 attributes 信息
        List<MarketGoodsAttribute> attributes = createGoodsBo.getAttributes();

        // 再将创建成功的商品加入产品列表中
        marketGoodsMapper.insertSelective(goods);

        if (specifications != null) {
            for (MarketGoodsSpecification specification : specifications) {
                specification.setAddTime(new Date());
                specification.setGoodsId(goods.getId());
                specification.setUpdateTime(new Date());
                marketGoodsSpecificationMapper.insertSelective(specification);
            }

        }
        if (products != null) {
            for (MarketGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(goods.getId());
                marketGoodsProductMapper.insertSelective(product);
            }
        }
      if (attributes != null) {
          for (MarketGoodsAttribute attribute : attributes) {
              attribute.setAddTime(new Date());
              attribute.setGoodsId(goods.getId());
              attribute.setUpdateTime(new Date());
              marketGoodsAttributeMapper.insertSelective(attribute);
          }
      }

        String id = goods.getGoodsSn();
        String name = goods.getName();
        BaseRespVo vo = new BaseRespVo();

        if (id == null || name == null){
            vo.setErrmsg("参数不对");
            vo.setErrno(401);
            return vo;
        }
        vo.setErrmsg("成功");
        vo.setErrno(0);
        return vo;
    }
    @Transactional
    @Override
    public void deleteGoods(MarketGoods marketGoods) {
        Integer id = marketGoods.getId();
        /*MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);*/
        int i = marketGoodsMapper.deleteByPrimaryKey(id);
        System.out.println(i);
    }

    @Override
    public void updateGoods(UpdateGoodsBo updateGoodsBo) {
        // 通过传入的参数获取goods信息
        MarketGoods goods = updateGoodsBo.getGoods();
        // 再获取 specification
        List<MarketGoodsSpecification> specifications = updateGoodsBo.getSpecifications();
        // 获取 product信息
        List<MarketGoodsProduct> products = updateGoodsBo.getProducts();
        // 获取 attributes 信息
        List<MarketGoodsAttribute> attributes = updateGoodsBo.getAttributes();
        String[] gallery = goods.getGallery();
        marketGoodsMapper.updateByPrimaryKeySelective(goods);

      if (specifications != null){
          for (MarketGoodsSpecification specification : specifications) {
              specification.setUpdateTime(new Date());
              marketGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
          }
      }
      if (products != null){
          for (MarketGoodsProduct product : products) {
              product.setUpdateTime(new Date());
              marketGoodsProductMapper.updateByPrimaryKeySelective(product);
          }
      }
      if (attributes != null){
          for (MarketGoodsAttribute attribute : attributes) {
              attribute.setUpdateTime(new Date());
              marketGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
          }
      }


    }

}

